package com.xiaohong.goods.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.goods.pojo.SpecParam;
import com.xiaohong.goods.pojo.Spu;
import com.xiaohong.goods.service.SpecParamService;
import entity.Result;
import entity.StatusCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Tag(name = "SpecParam接口文档")
@RestController
@RequestMapping("/specParam")
@CrossOrigin
public class SpecParamController {

    @Autowired
    private SpecParamService specParamService;

    /***
     * 分页条件搜索实现SpecParam
     * @param specParam
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询SpecParam",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "SpecParam对象",required = true) SpecParam specParam, @PathVariable int page, @PathVariable  int size){
        //调用SpecParamService实现分页条件查询SpecParam
        Page pageInfo = specParamService.findPage(specParam, page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 分页搜索实现SpecParam
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询SpecParam",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SpecParamService实现分页查询SpecParam
        Page pageInfo = specParamService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索SpecParam数据
     * @param specParam
     * @return
     */
    @Operation(summary = "多条件查询SpecParam",description = "所需权限：")
    @PostMapping(value = "/search" )
    public Result<List<SpecParam>> findList(@RequestBody(required = false) @Parameter(name = "SpecParam对象",required = true) SpecParam specParam){
        //调用SpecParamService实现条件查询SpecParam
        List<SpecParam> list = specParamService.findList(specParam);
        return new Result<List<SpecParam>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据Id删除specParam数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除SpecParam",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用SpecParamService实现根据ID删除
        specParamService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 根据ID修改SpecParam数据
     * @param specParam
     * @param id
     * @return
     */
    @Operation(summary = "根据ID修改SpecParam",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @PutMapping(value = "/{id}" )
    public Result update(@RequestBody @Parameter(name = "SpecParam对象",required = true) SpecParam specParam,@PathVariable Integer id){
        // 补充id
        specParam.setId(id);
        // 添加单位
        String[] split = specParam.getOptions().split(",");
        for (int i = 0; i < split.length; i++) {
            split[i] += specParam.getUnit();
        }
        List list = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            list.add(split[i]);
        }
        specParam.setOptionsAndUnit(list.toString()
                .replace("[","").replace("]","").replace(", ",","));
        //调用SpecParamService实现修改SpecParam
        specParamService.update(specParam);
        return new Result(true,StatusCode.OK,"修改成功",specParam);
    }

    /***
     * 新增SpecParam数据
     * @param specParam
     * @return
     */
    @Operation(summary = "添加SpecParam",description = "所需权限：")
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "SpecParam对象",required = true) SpecParam specParam){
        //调用SpecParamService实现添加SpecParam
        if (specParamService.findList(specParam).size() != 0){
            return new Result(true,StatusCode.PARAMETERERROR,"重复添加！");
        }
        // 添加单位
        String[] split = specParam.getOptions().split(",");
        for (int i = 0; i < split.length; i++) {
            split[i] += specParam.getUnit();
        }
        List list = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            list.add(i, split[i]);
        }
        specParam.setOptionsAndUnit(list.toString()
                .replace("[","").replace("]","").replace(", ",","));
        specParamService.add(specParam);
        // 这里specParam由于时Id类型是AUTO,所以添加前就有id了
        return new Result(true,StatusCode.OK,"添加成功",specParam);
    }

    /***
     * 根据ID查询SpecParam数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询SpecParam",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @GetMapping("/{id}")
    public Result<SpecParam> findById(@PathVariable Integer id){
        //调用SpecParamService实现根据ID查询SpecParam
        SpecParam specParam = specParamService.findById(id);
        return new Result<SpecParam>(true,StatusCode.OK,"查询成功",specParam);
    }

    /***
     * 查询SpecParam全部数据
     * @return
     */
    @Operation(summary = "查询所有SpecParam",description = "所需权限：")
    @GetMapping
    public Result<List<SpecParam>> findAll(){
        //调用SpecParamService实现查询所有SpecParam
        List<SpecParam> list = specParamService.findAll();
        return new Result<List<SpecParam>>(true, StatusCode.OK,"查询成功",list) ;
    }

    /***
     * 根据前端传来的数组转换成规格参数Map传回
     * @return
     */
    @Operation(summary = "查询所有Spu",description = "所需权限：")
    @PostMapping("/test")
    public Result<Map<String,String>> makeToString(@RequestBody ArrayList<String> list){
        Map<String, String> map = new HashMap<>();
        // 遍历数组
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) != null) {
                // 查询对应下标加一查规格名,加入Map
                map.put(specParamService.findById(i + 1).getName(), list.get(i));
            }
        }
        return new Result<Map<String, String>>(true, StatusCode.OK,"查询成功",map);
    }

    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list);
        System.out.println(list.toString());
        System.out.println(list.toString().replace("[","").replace("]",""));
    }
}
